<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Modules\Admin\Models\AuthRule as AuthRuleModel;
use Modules\Admin\Models\AuthGroup as AuthGroupModel;
use Modules\Admin\Models\Admin as AdminModel;
use Modules\Admin\Http\Requests\AdminUpdatePwdRequest;
use Illuminate\Support\Facades\Auth;
class IndexController extends CommonController
{
	/**
     * 首页
     */
    public function index(Request $request)
    {
		$id = Auth::guard("admin")->user()->id;
		$group_id = Auth::guard("admin")->user()->group_id;
		$auth_rule = Cache::get('auth_rule');
		$admin_rules_str = AuthGroupModel::where('id',$group_id)->value('rules');
		$admin_rules = array_filter(explode('|',$admin_rules_str));
		if(!$auth_rule){
			$auth_rule = AuthRuleModel::where('status',1)->orderBy('sort','asc')->orderBy('id','asc')->get()->toArray();
			Cache::put('auth_rule',$auth_rule,3600);
		}
		//声明数组
        $menus = array();
		if(!$module = $request->get('module')){
			$module = 'admin';
		};
		//比例数据
        foreach ($auth_rule as $key=>$val){
            //判断是否为顶级目录
			if($val['pid']==0 && $val['href'] == $module){
				if($id != 1){
					//给出该管理员的权限
                    if(in_array($val['id'],$admin_rules)){
                        $menus[] = $val;
                    }
                }else{
					//超级管理员拥有所有权限
                    $menus[] = $val;
                }
            }
			
        }
		//便利顶级目录
        foreach ($menus as $k=>$v){
            //便利所有的目录
			foreach ($auth_rule as $kk=>$vv){
                //找出子级目录
				if($v['id']==$vv['pid']){
					//组合跳转路径
					$vv['href'] = $vv['href']?route($vv['href']):'';
                    if($id != 1) {
						//给出应有权限
                        if (in_array($vv['id'],$admin_rules)) {
                            $menus[$k]['children'][] = $vv;
                        }
                    }else{
						//给出所有权限
                        $menus[$k]['children'][] = $vv;
                    } 
                }
            }
        }
		$menus = json_encode($menus,true);
        return view('admin::index/index',compact('menus','module'));
    }
	/**
     * 控制面板
     */
    public function main()
    {
        return view('admin::index/main');
    }
	/**
     * 退出登录
     */
    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect(route('admin.login.index'));
    }
	/**
     * 修改密码页面
     */
    public function upadte_pwd_view()
    {
        return view('admin::index/upadte_pwd_view');
    }
	/**
     * 修改密码数据处理
     */
    public function upadte_pwd(AdminUpdatePwdRequest $request,$id)
    {
		return (new AdminModel())->admin_upadte_pwd($request->all(),$id,route('admin.index.main')); 
    }
	/**
     * 修改密码页面
     */
    public function clear()
    {
		deldir(storage_path('framework/views'));
		//deldir(storage_path('logs'));
		Cache::forget('auth_rule');
		Cache::forget('config');
		Cache::forget('blog_config');
		Cache::forget('blog_menus');
		Cache::forget('blog_link');
		Cache::forget('blog_label');
		return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => '清除成功！',
				'url'=> route('admin.index.index')
		],200);
		
    }
}
